﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using PWDManage.Model.OtherClass;
using PersonBlog.Common;

namespace PersonBlog.Controllers
{
    public class NBAInfoController : Controller
    {
        // GET: NBAInfo
        public ActionResult Index()
        {
            string start = DateTime.Now.ToString("yyyy-MM-dd");
            string end = DateTime.Now.AddDays(5).ToString("yyyy-MM-dd");
            string url = "http://matchweb.sports.qq.com/kbs/list?from=NBA_PC&columnId=100000&startTime="+ start + "&endTime="+ end + "&callback=ajaxExec&_=1482285677342";
            HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(url);
            httpRequest.Timeout = 2000;
            httpRequest.Method = "GET";
            HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse();
            StreamReader sr = new StreamReader(httpResponse.GetResponseStream(), System.Text.Encoding.GetEncoding("gb2312"));
            string result = sr.ReadToEnd();
            result = result.Replace("\r", "").Replace("\n", "").Replace("\t", "");
            result = result.Replace("(", "").Replace(")", "").Replace("ajaxExec", "");
            List<NBAScdule> nbaScduleList = new List<NBAScdule>();
            JObject jo = JObject.Parse(result);
            var v = jo["data"].ToString();
            JObject joChild = JObject.Parse(v.ToString());
            for (int i = 0; i < 5; i++)
            {
                string key = DateTime.Now.AddDays(i).ToString("yyyy-MM-dd");
                if (joChild.Property(key)==null)
                {
                    continue;
                }
                var vc = joChild[key].ToString();
                List<NbaResult> list = JsonConvert.DeserializeObject<List<NbaResult>>(vc.ToString());
                NBAScdule nbaScdule = new NBAScdule();
                DateTime time = Convert.ToDateTime(key);
                string week=TimeParser.WeekChange(time.DayOfWeek.ToString());
                nbaScdule.date = time.ToString("MM月dd日")+" "+week;
                nbaScdule.list = list;
                nbaScduleList.Add(nbaScdule);
            }
            ViewBag.NBASCD = nbaScduleList;
            return View();
        }

        public ActionResult GetInfo()
        {
            string url = "http://matchweb.sports.qq.com/kbs/list?from=NBA_PC&columnId=100000&startTime=2016-12-21&endTime=2016-12-21&callback=ajaxExec&_=1482285677342";
            HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(url);
            httpRequest.Timeout = 2000;
            httpRequest.Method = "GET";
            HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse();
            StreamReader sr = new StreamReader(httpResponse.GetResponseStream(), System.Text.Encoding.GetEncoding("gb2312"));
            string result = sr.ReadToEnd();
            result = result.Replace("\r", "").Replace("\n", "").Replace("\t", "");
            result = result.Replace("(", "").Replace(")", "").Replace("ajaxExec", "");
            return Content(result);
        }
    }
}